package service4user

import (
	models2 "Gin-Gorm-boke/boke_common/models"
	"Gin-Gorm-boke/boke_common/response"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/jinzhu/copier"
	"gorm.io/gorm"
	"strconv"
)

type CommentCo struct {
	gorm.Model
	Id                int            `gorm:"column:id;" json:"id"`
	Type              null.Int       `gorm:"column:type;default:0" json:"type"`
	ArticleId         int            `gorm:"column:article_id;" json:"articleId"`
	RootId            int            `gorm:"column:root_id;default:-1" json:"rootId"`
	Content           string         `gorm:"column:content;" json:"content"`
	ToCommentUserId   int            `gorm:"column:to_comment_user_id;default:-1" json:"toCommentUserId"`
	ToCommentId       int            `gorm:"column:to_comment_id;default:-1" json:"toCommentId"`
	ToCommentUserName string         `gorm:"column:to_comment_user_name;"json:"toCommentUserName"`
	Username          string         `gorm:"column:username;"json:"username"`
	CreateTime        models2.MyTime `gorm:"column:created_at;" json:"createTime"`
	CreateBy          int            `gorm:"column:create_by;" json:"createBy"`
	UpdateBy          int            `gorm:"column:update_by;" json:"updateBy"`
	Children          []CommentCo    `json:"children"`
}

func GetCommentList(c *gin.Context) {
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	ArticleId, _ := strconv.Atoi(c.Query("articleId"))

	db := models2.Db.Model(new(models2.CommentVo)).
		Table("sg_comment c").
		Select("c.*,u.nick_name as username,u2.nick_name as to_comment_user_name").
		Joins("LEFT JOIN sys_user u ON c.create_by = u.id").
		Joins("LEFT JOIN sys_user u2 ON c.to_comment_user_id = u2.id")

	if ArticleId != 0 {
		db.Where("article_id=?", ArticleId)
	}
	db.Where("c.root_id = ?", -1)
	var total int64
	db.Count(&total)
	var commentList []CommentCo
	var commentVoList []CommentCo
	db.Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&commentList)
	copier.Copy(&commentVoList, &commentList)

	for i := range commentVoList {
		var children []CommentCo
		dbc := models2.Db.Model(new(models2.Comment)).
			Table("sg_comment c").
			Select("c.*,u.nick_name as username,u2.nick_name as to_comment_user_name").
			Joins("LEFT JOIN sys_user u ON c.create_by = u.id").
			Joins("LEFT JOIN sys_user u2 ON c.to_comment_user_id = u2.id").
			Order("c.created_at desc")
		dbc.Where("c.root_id=?", commentVoList[i].Id).Find(&children)
		fmt.Println(children)
		commentVoList[i].Children = children

	}
	response.ResponseOK(c, gin.H{
		"total": total,
		"rows":  commentVoList,
	})
	return
}

func GetLinkCommentList(c *gin.Context) {
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))

	db := models2.Db.Model(new(models2.CommentVo)).
		Table("sg_comment c").
		Select("c.*,u.nick_name as username,u2.nick_name as to_comment_user_name").
		Joins("LEFT JOIN sys_user u ON c.create_by = u.id").
		Joins("LEFT JOIN sys_user u2 ON c.to_comment_user_id = u2.id")

	db.Where("c.root_id = ?", -1)
	db.Where("c.type=?", 1)
	var total int64
	db.Count(&total)
	var commentList []CommentCo
	var commentVoList []CommentCo
	db.Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&commentList)
	copier.Copy(&commentVoList, &commentList)

	for i := range commentVoList {
		var children []CommentCo
		dbc := models2.Db.Model(new(models2.Comment)).
			Table("sg_comment c").
			Select("c.*,u.nick_name as username,u2.nick_name as to_comment_user_name").
			Joins("LEFT JOIN sys_user u ON c.create_by = u.id").
			Joins("LEFT JOIN sys_user u2 ON c.to_comment_user_id = u2.id").
			Order("c.created_at desc")
		dbc.Where("c.root_id=?", commentVoList[i].Id).Find(&children)
		fmt.Println(children)
		commentVoList[i].Children = children

	}
	response.ResponseOK(c, gin.H{
		"total": total,
		"rows":  commentVoList,
	})
	return
}
func AddComment(c *gin.Context) {
	var comment models2.Comment
	if err := c.ShouldBindJSON(&comment); err != nil {
		response.ResponseBadRequest(c)
		return
	}
	if comment.Content == "" {
		response.ResponseBadRequest(c)
		return
	}
	//userId, _ := c.Get("userId")
	//comment.CreateBy = userId.(int)
	//comment.UpdateBy = userId.(int)

	models2.Db.WithContext(c).Create(&comment)
	response.ResponseOK(c, nil)
	return

}
